import 'package:flutter/material.dart';
import 'Equtil.dart';

class EqGridView extends CustomPainter {
  late double times = 0;
  late int mBottom = 0;
  late int mHeight;
  late int mTop = 0;
  late int mWidth;

  late int dbCount;
  late List<double> fcValue;
  late List<String> gridHz;
  late List<String> griddB;
  late int sBottom;
  late int sLeft;
  late int sRight;
  late int sTop;
  late List<double> dbGrid;
  late List<double> fcGrid;
  late Color backgroundColor;

  ///坐标值颜色
  Color? axisColor;

  EqGridView({this.axisColor = Colors.white, super.repaint}) {
    _initData();
  }

  @override
  void paint(Canvas canvas, Size size) {
    if (size.width >= 1 && size.height >= 1) {
      this.times = -0.5555556 * (size.height - 10 - 20);
      reGird(size.width.toInt(), size.height.toInt());
      this.mWidth = size.width.toInt();
      this.mHeight = size.height.toInt();
    }
    drawGrid(canvas, size);
    print('draw grid $size');
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  void _initData() {
    this.dbCount = 36;
    this.fcValue = [
      20,
      30,
      40,
      50,
      60,
      70,
      80,
      90,
      100,
      200,
      300,
      400,
      500,
      600,
      700,
      800,
      900,
      1000,
      2000,
      3000,
      4000,
      5000,
      6000,
      7000,
      8000,
      9000,
      10000,
      20000,
      22000
    ];
    this.gridHz = [
      "20",
      "30",
      "40",
      "50",
      "60",
      "70",
      "80",
      "90",
      "100",
      "200",
      "300",
      "400",
      "500",
      "600",
      "700",
      "800",
      "900",
      "1k",
      "2k",
      "3k",
      "4k",
      "5k",
      "6k",
      "7k",
      "8k",
      "9k",
      "10k",
      "20k",
      "22k"
    ];
    this.griddB = ["20", "15", "10", "5", "0dB", "-5", "-10", "-15", "-20"];
    this.sBottom = 20;
    this.sLeft = 30;
    this.sRight = 10;
    this.sTop = 10;
    this.dbGrid = [];
    for (int i = 0; i < this.griddB.length; i++) {
      this.dbGrid.add(0);
    }
    this.fcGrid = [];
    for (int i = 0; i < this.fcValue.length; i++) {
      this.fcGrid.add(0);
    }
    this.backgroundColor = Colors.black;
  }

  void drawText(String text, Canvas canvas, Offset callback(Size textSize)) {
    final textStyle = TextStyle(
      color: axisColor,
      fontSize: 6,
    );
    final textSpan = TextSpan(
      text: text,
      style: textStyle,
    );
    final textPainter = TextPainter(
        text: textSpan,
        textDirection: TextDirection.ltr,
        textAlign: TextAlign.center);
    textPainter.layout(minWidth: 0, maxWidth: 60);
    Offset drawPosition = callback(Size(textPainter.width, textPainter.height));
    textPainter.paint(canvas, drawPosition);
    return;
    // final textStyle = ui.TextStyle(
    //   color: Colors.grey,
    //   fontSize: 12,
    // );
    // final paragraphStyle = ui.ParagraphStyle(
    //   textDirection: TextDirection.ltr,
    // );
    // final paragraphBuilder = ui.ParagraphBuilder(paragraphStyle)
    //   ..pushStyle(textStyle)
    //   ..addText(text);
    // final constraints = ui.ParagraphConstraints(width: 300);
    // final paragraph = paragraphBuilder.build();
    // paragraph.layout(constraints);

    // Offset position = callback(Size(paragraph.width, paragraph.height));
    // print('size ' + paragraph.width.toString());
    // canvas.drawParagraph(paragraph, position);
  }

  void drawGrid(Canvas canvas, Size var1) {
    Size var3;
    if (var1.width >= 1 && var1.height >= 1) {
      int var2;
      //网格颜色
      final Paint paint = Paint()..color = Colors.grey; // Color(0xff2a9ccb);
      paint.strokeWidth = 1.0;
      paint
        ..style = PaintingStyle.stroke
        ..strokeCap = StrokeCap.round;
      paint..strokeJoin = StrokeJoin.round;

      final path = Path();

      for (var2 = 0; var2 < this.dbGrid.length; ++var2) {
        // var3 = [this.griddB[var2] sizeWithAttributes:{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
        // [this.griddB[var2] drawAtPoint:CGPointMake(30 - var3.height - 10, [this.dbGrid[var2] + var3.height / 2-13) withAttributes:{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor whiteColor]}];
        drawText(this.griddB[var2], canvas, (var3) {
          return Offset(
              30 - var3.height - 10, this.dbGrid[var2] + var3.height / 2 - 13);
        });
        path.moveTo(30, this.dbGrid[var2]);
        path.lineTo(this.mWidth.toDouble() - 10, this.dbGrid[var2]);
      }

      for (var2 = 0; var2 < this.fcGrid.length; ++var2) {
        // var3 = [this.gridHz[var2] sizeWithAttributes:{NSFontAttributeName:[UIFont systemFontOfSize:12]}];

        if (var2 != 8 && var2 != 17 && var2 != 26) {
          path.moveTo(this.fcGrid[var2], this.mTop.toDouble());
          path.lineTo(this.fcGrid[var2], this.mBottom.toDouble());
          if (var2 == 0 || var2 == 28) {
            // [this.gridHz[var2] drawAtPoint:CGPointMake([this.fcGrid[var2] - var3.width / 2, this.mBottom + var3.height + 5-15) withAttributes:{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor whiteColor]}];
            drawText(this.gridHz[var2], canvas, (var3) {
              return Offset(this.fcGrid[var2] - var3.width / 2,
                  this.mBottom + var3.height + 5 - 10);
            });
          }
        } else {
          path.moveTo(this.fcGrid[var2], this.mTop.toDouble());
          path.lineTo(this.fcGrid[var2], this.mBottom.toDouble());
          // [this.gridHz[var2] drawAtPoint:CGPointMake([this.fcGrid[var2] - var3.width / 2, this.mBottom + var3.height + 5-15) withAttributes:{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor whiteColor]}];
          drawText(this.gridHz[var2], canvas, (var3) {
            return Offset(this.fcGrid[var2] - var3.width / 2,
                this.mBottom + var3.height + 5 - 10);
          });
        }
      }
      canvas.drawPath(path, paint);
    }
  }

  void reGird(int var1, int var2) {
    int var3 = var2 - 10 - 20;

    for (var2 = 0; var2 < this.fcGrid.length; ++var2) {
      this.fcGrid[var2] =
          Equtil.fc2int(var1 - 30 - 10, 0, this.fcValue[var2].toInt())
                  .toDouble() +
              30;
    }

    var1 = var3 ~/ (this.dbGrid.length - 1);
    this.dbGrid[4] = (var3 / 2 + 10);

    // this.dbGrid[4] = this.dbGrid[4] - var1 * 2;
    this.dbGrid[3] = (this.dbGrid[4] - var1);
    this.dbGrid[2] = (this.dbGrid[4] - var1 * 2);
    this.dbGrid[1] = (this.dbGrid[4] - var1 * 3);
    this.dbGrid[0] = (this.dbGrid[4] - var1 * 4);

    this.dbGrid[5] = (this.dbGrid[4] + var1);
    this.dbGrid[6] = (this.dbGrid[4] + var1 * 2);
    this.dbGrid[7] = (this.dbGrid[4] + var1 * 3);
    this.dbGrid[8] = (this.dbGrid[4] + var1 * 4);
    /*        this.dbGrid[9] = this.dbGrid[6] + var1 * 3;
     this.dbGrid[10] = this.dbGrid[6] + var1 * 4;
     this.dbGrid[11] = this.dbGrid[6] + var1 * 5;
     this.dbGrid[12] = this.dbGrid[6] + var1 * 6;*/
    this.mTop = this.dbGrid[0].toInt();
    this.mBottom = this.dbGrid[8].toInt();
  }
}
